<template>
    <ContentWrap>
		<el-space wrap>
            <mdp-select-cate v-model="filters['categoryId']" width="8em" clearable filterable placeholder="分类" />  
            <mdp-select-tag v-model="filters['tagIds']" width="8em" clearable filterable placeholder="标签" />  
			<el-input v-model="filters.id" style="width: 20%;" placeholder="编号" clearable title="支持>、<、 >=、<=、!=、*字符*、$IS NULL、$IN 1,2,3、$between 1,5等操作符"/>
            <el-input v-model="filters.formName" style="width: 20%;" placeholder="名称" clearable title="支持>、<、 >=、<=、!=、*字符*、$IS NULL、$IN 1,2,3、$between 1,5等操作符"/>
            <el-input v-model="filters.username" style="width: 20%;" placeholder="创建人" clearable title="支持>、<、 >=、<=、!=、*字符*、$IS NULL、$IN 1,2,3、$between 1,5等操作符"/>

            <el-button v-loading="load.list" :disabled="load.list==true" @click="searchTableDatas()" icon="search" type="primary">查询</el-button>
			
            <span  v-if="currOpType=='select' &&  this.multiple==true">
                <el-button :disabled="disBtn('selectBtn') || this.sels.length===0" type="primary" @click="selectListConfirm" icon="check"/>
            </span>
		</el-space>
        <template #header>
		<el-space wrap> 
            <mdp-hi-query :column-configs="columnConfigs" v-model="hiQueryParams" @change="onHiQueryParamsChange"/>
            <el-button icon="zoom-out" @click="searchReset()" title="重置查询条件"/> 
            <span  v-if="currOpType=='mng'">
                <el-button :disabled="disBtn('addBtn') || !checkBtnQx('addBtn',menuDefId) " type="primary" @click="toDesignAdd()" icon="plus"/>
             </span>
            <el-button icon="download" @click="export2Excel()" title="导出当前结果" />
            <mdp-table-configs :column-configs="columnConfigs" v-model="checkedColumns" :form-def-id="expandId"/>
		</el-space>
        </template>
    </ContentWrap> 
			<!--列表 FormDef 表单定义-->
			<el-table v-adaptive :ref="refId+'Table'"  :data="tableDatas" @sort-change="sortChange" highlight-current-row v-loading="load.list" border @selection-change="selsChange" @row-click="rowClick" style="width: 100%;">
				<el-table-column  type="selection" width="55" show-overflow-tooltip fixed="left" v-if="currOpType=='mng' || this.multiple==true"/>
				<el-table-column sortable type="index" width="55" show-overflow-tooltip  fixed="left"/>
				
				<el-table-column prop="formName"  label="表单名称" min-width="220" fixed="left" show-overflow-tooltip col-type="String" v-if="showCol('formName')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.formName" :maxlength="40" @change="editSomeFields(scope.row,'formName',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>
                <el-table-column prop="id" label="编号" min-width="120" show-overflow-tooltip  fixed="left" col-type="String" v-if="showCol('id')"/>
				
				
				<el-table-column prop="tagIds"  label="标签" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('tagIds')">
				    <template #default="scope">
                            <mdp-select-tag show-style="tag" v-model="scope.row.tagIds" multiple split="," :maxlength="255" @change="editSomeFields(scope.row,'tagIds',$event)" :disabled="disField"/>
				    </template>
				</el-table-column> 
				<el-table-column prop="username"  label="创建人姓名" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('username')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.username" :maxlength="255" @change="editSomeFields(scope.row,'username',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>
				<el-table-column prop="deptName"  label="创建部门" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('deptName')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.deptName" :maxlength="255" @change="editSomeFields(scope.row,'deptName',$event)" :disabled="disField"/>
				    </template>
				</el-table-column> 
				<el-table-column prop="branchId"  label="机构编号" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('branchId')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.branchId" :maxlength="40" @change="editSomeFields(scope.row,'branchId',$event)" :disabled="disField"/>
				    </template>
				</el-table-column> 
				<el-table-column prop="isTemplate"  label="是否为模板" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('isTemplate')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.isTemplate" :maxlength="1" @change="editSomeFields(scope.row,'isTemplate',$event)" :disabled="disField"/>
				    </template>
				</el-table-column> 
				<el-table-column prop="ctime"  label="创建日期" min-width="120" show-overflow-tooltip col-type="Date" v-if="showCol('ctime')">
				    <template #default="scope">
                            <mdp-date type="date" placeholder="选择日期" show-style="tag" v-model="scope.row.ctime" @change="editSomeFields(scope.row,'ctime',$event)"  value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD" :disabled="disField"/>
				    </template>
				</el-table-column>
                <el-table-column prop="userid"  label="创建人" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('userid')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.userid" :maxlength="40" @change="editSomeFields(scope.row,'userid',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>
				<el-table-column prop="deptid"  label="创建部门" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('deptid')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.deptid" :maxlength="40" @change="editSomeFields(scope.row,'deptid',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>
				<el-table-column prop="categoryId"  label="分类编号" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('categoryId')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.categoryId" :maxlength="50" @change="editSomeFields(scope.row,'categoryId',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>
				<el-table-column prop="tagIds"  label="标签编号列表" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('tagIds')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.tagIds" :maxlength="255" @change="editSomeFields(scope.row,'tagIds',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>
				<el-table-column prop="tagNames"  label="标签名字列表" min-width="120" show-overflow-tooltip col-type="String" v-if="showCol('tagNames')">
				    <template #default="scope">
                            <mdp-input show-style="tag" v-model="scope.row.tagNames" :maxlength="255" @change="editSomeFields(scope.row,'tagNames',$event)" :disabled="disField"/>
				    </template>
				</el-table-column>    
				<el-table-column label="操作" :width="currOpType=='mng'?320:200" fixed="right">
				    <template #header>
                        
                    </template>
				    <template #default="scope" v-if="currOpType=='mng'"  >
				        <el-button :disabled="disBtn('editBtn') || !checkBtnQx('editBtn',menuDefId)  || !checkFormDefQx(scope.row,'editBtn') " type="primary" @click="toDesign(scope.row)" icon="edit" title="修改一条数据">设计</el-button>
 				        <el-button :disabled="disBtn('delBtn') || !checkBtnQx('delBtn',menuDefId)  || !checkFormDefQx(scope.row,'editBtn') " type="danger" @click="handleDel2(scope.row,scope.$index)" icon="delete"  title="删除一条数据"/>
                        <el-button :disabled="disBtn('editBtn') || !checkBtnQx('editBtn',menuDefId) || !checkFormDefQx(scope.row,'editBtn') " @click="toFill(scope.row)">填单</el-button>
                        <el-button @click="openRoute(scope.row)">数据</el-button>
                            
                    </template>

				    <template #default="scope" v-else-if="currOpType=='list'" >
				        <el-button   type="primary" @click="openForm({parentOpType:currOpType,subOpType:'detail',formData:scope.row})" icon="view"/>
 				    </template>

				    <template #default="scope" v-else-if="currOpType=='select' && this.multiple!=true" >
				        <el-button   type="primary" @click="selectConfirm(scope.row)" icon="check"/>
 				    </template>
				</el-table-column>
                
                
                <template v-for="(field,idx) in formFields" :key="idx">
                    <el-table-column   :prop="expandFieldName+'.'+field.fieldIdCamel" :label="field.fieldTitle">
                        <template #default="scope">
                            <div v-if="scope.row[expandFieldName]">{{ scope.row[expandFieldName][field.fieldIdCamel] }}</div>
                        </template>
                    </el-table-column>
                 </template>
			</el-table>  
			<el-pagination class="padding-top"
				layout="slot,total, sizes, prev, next,pager,jumper"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				:page-sizes="[10,20, 50, 100, 500]"
				:current-page="pageInfo.pageNum"
				:page-size="pageInfo.pageSize"
				:total="pageInfo.total"
				
			/>  
            <mdp-dialog ref="formQxDialog" title="表单权限设置" width="60%" >
                <template #default="{visible,data,dialog}">
                    <form-qx-set :form-def="data.formData" :visible="visible" @cancel="dialog.close()" />
                </template>
            </mdp-dialog>  
</template>

<script>

import { MdpTableMixin } from '@/components/mdp-ui/mixin/MdpTableMixin.js';
import * as FormDefApi from '@/api/mdp/form/formDef';
import { mapState } from 'pinia'
import { useUserStore } from '@/store/modules/user' 

export default {
    name:'formDefMng',
    mixins:[MdpTableMixin], 
    computed: {
        ...mapState(useUserStore,['userInfo'])
    },
    watch:{
        $route(to){
            this.initCurrData();
        }
    },
    data() {
        return {
            menuDefId:'',//menu_def.id 菜单表菜单编号，用于按钮权限判断
            menuDefName:'表单定义',//menu_def.name 功能名称，用于导出excel等文件名
            expandId:'form_def',
            refId:'formDef',//引用编号，<table :ref="refId+'Table'"> <form :ref="refId+'Form'">
            pkNames:["id"],//表格主键的java属性名称，驼峰命名，默认为id,支持多主键
            
            filters:{//查询参数

            },
            defaultFilters:{//默认查询参数,第一次打开界面的时候用到，恢复默认值的时候用到

            },

            defaultCheckColumnNum:8,//默认展示的表格列数，前8列

            editable:false,//是否可编辑模式

            //增删改查(含批量)接口
            apis:{
                list: FormDefApi.listFormDef,
                add: FormDefApi.addFormDef,
                del: FormDefApi.delFormDef,
                edit: FormDefApi.editFormDef,
                editSomeFields: FormDefApi.editSomeFieldsFormDef,
                batchAdd: FormDefApi.batchAddFormDef,
                batchDel: FormDefApi.batchDelFormDef,
                batchEdit: FormDefApi.batchEditFormDef,
            }
        }
    },
    methods: {
         //页面初始化需要配置的特殊逻辑写这里
          initCurrData(){
            if(this.$route.params.expandId){ 
                this.addForm.id=this.$route.params.expandId
            }else{
                this.addForm.id=''
            }
             this.searchTableDatas();
          },

          /**
           * 检查参数是否满足调用后台接口的条件
           *
           * @param params 提交给后台的参数池,map类型
           * @returns true / false
           */
          preQueryParamCheck(params){
            if(this.$route.params.expandId){
                params.id=this.$route.params.expandId
            }
              return true;
          },

          //页面数据加载完后需要对数据进行加工处理的
          afterList(datas,isOk,apiName){ 
          
            var extInfos={id:'',two:'5',three:'4',four:'3',five:'11',six:'',seven:'5',eight:'',nine:'',ten:'',lastTime:'',one:'',zero:'',remark:'',formId:'',userid:'',eleven:'',twelve:'',thirteen:'',fourteen:'',fifteen:'',attachmentUrls:'',attachmentNames:'',branchId:'',bizKey:'',deptid:'',flowState:'',createTime:'',dqxCode:'',cuserid:'',tagIds:'',tagNames:'',cusername:'',deptName:''}

             datas.forEach(d=>{ 
                d.extInfos=extInfos
             })
             
          },

          /**
           * 对修改的字段进行判断，返回false ,将取消更新数据库
           * @param {*} row 当前选中的行
           * @param {*} fieldName 修改的字段名
           * @param {*} $event 修改后的值
           * @param {*} params 将要提交服务器的参数
           * @returns true/false 返回false ,将取消更新数据库
           */
          editSomeFieldsCheck(row,fieldName,$event,params){
              if(this.currOpType=='add'||this.currOpType=='detail'){
                  return false;
              }
              params[fieldName]=$event
              return true;
          },

          checkFormDefQx(formDef,btnId){
            if(formDef.userid==this.userInfo.userid){
                return true;
            }else{
                return false;
            }
          }, 
        openRoute(data){ 
            if(!data.options){
                this.$message.error("未配置接口参数，不支持直接查看数据")
                return;
            }
            var options=JSON.parse(data.options)
            if(!options.form.crudApisCfg){
                this.$message.error("未配置接口参数，不支持直接查看数据")
                return;
            }
            var crudApisCfg=JSON.parse(options.form.crudApisCfg)
            if(!crudApisCfg.list){
                this.$message.error("未配置列表接口参数，不支持直接查看数据")
                return;
            }
            
            if(crudApisCfg.list.apiType=='2'){
                
                if(!crudApisCfg.list.params||!crudApisCfg.list.params.tableName){
                    this.$message.error("未配置列表接口参数，不支持直接查看数据")
                    return;
                }
                var tableName=crudApisCfg.list.params.tableName
                var dataSource=crudApisCfg.list.params.dataSource
                this.$router.push({path:`/mdp/dm/data/list/${dataSource}/${tableName}`})
            }else if(crudApisCfg.list.apiType=='3'){
                if(!crudApisCfg.list.params||!crudApisCfg.list.params.dataSetId){
                    this.$message.error("未配置列表接口参数，不支持直接查看数据")
                    return;
                }
                var dataSetId=crudApisCfg.list.params.dataSetId 
                this.$router.push({path:`/mdp/dm/data/set/list/${dataSetId}`})
            }else if(crudApisCfg.list.apiType=='1'){
                this.$message.error("基于普通api的接口参数，不支持直接查看数据")
            }
         },
        
        toDesignAdd(){ 
            if(!this.$route.params.expandId){
                this.$router.push({path:'/mdp/form/design/add'})
            }else{
                this.$router.push({path:'/mdp/form/design/detail/'+this.$route.params.expandId})
            }
           
        },
        toDesign(formDef){ 
            //this.$router.push({path:'/mdp/lcode/create'})
            this.$router.push({path:'/mdp/form/design/detail/'+formDef.id})
        },
        handleDel2(row,index){
            this.$mdp.removeFormFieldsCache(row.id)
            this.handleDel(row,index)
        },
        toFill(formDef){
            this.$router.push({path:'/mdp/form/data/add/'+formDef.id})
        }
    },
    mounted() {

    }
}

</script>

<style scoped>
</style>